package cn.chrilwe.autocreatetable.start.processor;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.support.DefaultLifecycleProcessor;
import org.springframework.stereotype.Component;

import cn.chrilwe.autocreatetable.start.config.Config.Mark;
import cn.chrilwe.autocreatetable.start.manager.AutoTableManager;

/**
 * spring容器完成初始化，执行建表逻辑后置处理器
 * @author chrilwe
 * 2020-8-6
 */
public class CustomLifecycleProcessor extends DefaultLifecycleProcessor {

	private DataSource dataSource;
	
	private String entityPath; 
	
	public CustomLifecycleProcessor(DataSource dataSource, String entityPath) {
		this.dataSource = dataSource;
		this.entityPath = entityPath;
	}
	
	@Override
	public void onRefresh() {
		try {
			System.out.println("===========开始自动创建表============");
			Connection connection = dataSource.getConnection();
			AutoTableManager.create(entityPath, connection);
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
